package com.mszl.blog.controller;

import com.mszl.blog.utils.QiniuUtils;
import com.mszl.blog.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.util.UUID;

@RestController
@RequestMapping("upload")

public class UploadController {
    @Autowired
    private QiniuUtils qiniuUtils;

    //七牛云，云服务，上传照片
    @PostMapping
    public Result upload(@RequestParam("image")MultipartFile file){
        String originalFilename=file.getOriginalFilename();//.png
        String fileName= UUID.randomUUID().toString()+"."+ StringUtils.substringAfterLast(originalFilename,".");
        //放到七牛云服务器，降低宽带消耗
        boolean upload = qiniuUtils.upload(file, fileName);
        if(upload){
            return Result.success(QiniuUtils.url+fileName);
        }
        return Result.fail(20001,"上传失败");


    }
}
